package com.marquee;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;

import com.marquee.friends.Frag_Friends;
import com.marquee.util.BadgeView;
import com.marquee.util.TabBitmap;
import com.marquee.yourdates.Frag_YourDates;

public class HomeActivity extends FragmentActivity {

	static String LOG_TAG = "test";

	/* Tab identifiers */
	private static final String TAB_1 = "데이트";
	private static final String TAB_2 = "일정추가";
	private static final String TAB_3 = "친구";
	private static final String TAB_4 = "더보기";

	TabHost mTabHost;
	TabFactory factory = new TabFactory(this);

	DialogFragment dialog_Notification;
	
	BadgeView badge;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home_activity);

		initTab();
		initNotification();

	}

	private void initNotification() {

		
		
		
		ImageButton IB_notification = (ImageButton) findViewById(R.id.IB_notification);
		badge = new BadgeView(this, IB_notification);
		badge.setText("3");
		
		badge.show();

		IB_notification.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d(LOG_TAG, "["+getLocalClassName()+"]"+"onClick");
				dialog_Notification = new Dialog_Notification();

				dialog_Notification.show(getSupportFragmentManager(), "MyTAG");
				

				
			}
		});
	

		// View target = findViewById(R.id.default_target);

	}

	/*
	 * Initialize the tabs and set views and identifiers for the tabs
	 */
	private void initTab() {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setOnTabChangedListener(listener);
		mTabHost.setup();

		addTab(TAB_1, createTabDrawable(R.drawable.tab_schedule));
		addTab(TAB_2, createTabDrawable(R.drawable.tab_add_schedule));
		addTab(TAB_3, createTabDrawable(R.drawable.tab_friends));
		addTab(TAB_4, createTabDrawable(R.drawable.tab_setting));

		mTabHost.setCurrentTab(0);

		/*
		 for ( int tab = 0; tab < mTabHost.getTabWidget().getChildCount();  ++tab ) {
		  mTabHost.getTabWidget().getChildAt(tab).getLayoutParams().height =  60; }
		 */

	}

	/*
	 * TabChangeListener for changing the tab when one of the tabs is pressed
	 */
	TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
		public void onTabChanged(String tabId) {

			Fragment fragment = null;
			/* Set current tab.. */
			if (tabId.equals(TAB_1)) {
				fragment = new Frag_YourDates();

			} else if (tabId.equals(TAB_2)) {

				fragment = new Frag_AddDate();

			} else if (tabId.equals(TAB_3)) {
			//	fragment = new Frag_MyFriend();
				fragment = new Frag_Friends();

			} else if (tabId.equals(TAB_4)) {
				fragment = new Frag_Setting();

			}
			if (fragment != null)
				replaceFragment(fragment);

		}
	};

	private Drawable createTabDrawable(int resId) {
		Resources res = getResources();
		StateListDrawable states = new StateListDrawable();

		final Options options = new Options();
		options.inPreferredConfig = Config.ARGB_8888;

		Bitmap icon = BitmapFactory.decodeResource(res, resId, options);

		Bitmap unselected = TabBitmap.createUnselectedBitmap(res, icon);
		Bitmap selected = TabBitmap.createSelectedBitmap(res, icon);

		icon.recycle();

		states.addState(new int[] { android.R.attr.state_selected },
				new BitmapDrawable(res, selected));
		states.addState(new int[] { android.R.attr.state_enabled },
				new BitmapDrawable(res, unselected));

		return states;
	}

	private void addTab(String tag, Drawable drawable) {
		TabHost.TabSpec spec = mTabHost.newTabSpec(tag);
		spec.setIndicator(createTabIndicator(tag, drawable));

		spec.setContent(factory);

		mTabHost.addTab(spec);
	}

	private View createTabIndicator(String label, Drawable drawable) {
		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, mTabHost.getTabWidget(), false);

		TextView txtTitle = (TextView) tabIndicator
				.findViewById(R.id.text_view_tab_title);
		txtTitle.setText(label);

		LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) txtTitle
				.getLayoutParams();
		txtTitle.setLayoutParams(params);

		ImageView imgIcon = (ImageView) tabIndicator
				.findViewById(R.id.image_view_tab_icon);
		imgIcon.setImageDrawable(drawable);

		return tabIndicator;
	}

	class TabFactory implements TabHost.TabContentFactory {
		Context mCon;

		TabFactory(Context context) {
			mCon = context;
		}

		@Override
		public View createTabContent(String arg0) {
			return findViewById(android.R.id.tabcontent);

		}
	}

	public void replaceFragment(Fragment fragment) {

		getSupportFragmentManager().beginTransaction()
				.replace(android.R.id.tabcontent, fragment).commit();

	}

	public void addFragment(Fragment fragment) {

		getSupportFragmentManager().beginTransaction()
				.replace(android.R.id.tabcontent, fragment)
				.addToBackStack(null).commit();

	}

	@Override
	public void onBackPressed() {
		if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
			Builder d = new AlertDialog.Builder(this);
			d.setMessage("정말 종료하시겠어요?");
			d.setPositiveButton("예", new DialogInterface.OnClickListener() {

				public void onClick(DialogInterface dialog, int which) {
					// process전체 종료
					finish();
				}
			});
			d.setNegativeButton("아니오", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});

			d.show();

		} else {
			super.onBackPressed();
		}

	}

}
